package com.rr.service.impl;

import com.rr.service.AsyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.framework.AopContext;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

/**
 * @author zhuangcanrun
 * @Date 2021/12/02/0:30
 * @description
 **/

@Service
@Slf4j
public class AsyncServiceImpl implements AsyncService {
    @Async
    @Override
    public void asyncThreeSecondFunction() {
        log.info("开始睡3秒.....");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("3秒睡完了.....");
    }

    @Override
    public void toCallOwnerFunction1() {
        log.info("方法toCallOwnerFunction...调用方法asyncThreeSecondFunction");
        asyncThreeSecondFunction();
        log.info("方法toCallOwnerFunction...调用方法asyncThreeSecondFunction结束");
    }
    @Override
    public void toCallOwnerFunction2(){
        log.info("通过AopContext.currentProxy()获得代理对象");
        AsyncServiceImpl asyncService= (AsyncServiceImpl) AopContext.currentProxy();
        log.info("调用代理对象的异步方法");
        asyncService.asyncThreeSecondFunction();
        log.info("启用代理对象的异步方法结束");
    }

    @PostConstruct
    public void init(){
        log.info("asyncServiceImpl.postConstruct");
    }
}
